<?php
class SF_PromoteEvents_EventsController extends Mage_Core_Controller_Front_Action
{
	public function indexAction()
	{
		$this->loadLayout();
		
		$eventCode = $this->getRequest()->getParam('type');
		$eventTitle = Mage::getModel('promoteevents/events')->getCollection()
						->addFieldToFilter('event_code', $eventCode)
						->getFirstItem()
						->getData('event_title');
						
		$this->getLayout()->getBlock('head')->setTitle($this->__($eventTitle));
				$this->getLayout()->createBlock('promoteevents/event','promoteevents_event')
					 ->setTemplate('em_minifilterproducts/'.$eventCode.'.phtml');
		$this->renderLayout();
	}
	
	public function initImageAction()
	{
		
		
		$mediaUrl = Mage::getBaseUrl('media');
		$eventsProductData = $eventsProductCollection = Mage::getModel('promoteevents/product')->getCollection()
									   ->addFieldToFilter('p_id', 1)
									   ->addFieldToFilter('status', 1);
		$imageArr = array();
		foreach($eventsProductData as $val)
		{
			$imageArr[$val['product_sku']] = $val['product_image'];
		}
		$resource = Mage::getSingleton('core/resource');
		$writeConnection = $resource->getConnection('core_write');
		$homeUrlLength = strlen(Mage::getBaseUrl('media'));
		$products = Mage::helper('promoteevents')->getEvents('fashion_products_collection')->getProducts();
		foreach($products as $pro)
		{
			foreach($pro as $product)
			{
				$imgFullUrl = Mage::helper('catalog/image')->init($product, 'small_image')->resize(190);
				$imgBasePath = substr($imgFullUrl,$homeUrlLength);
				$imgFinalPath = str_replace('cache','events',$imgBasePath);
				echo $imgFullUrl .'<br>';
				
				$query = "UPDATE `rumall_promote_events_product` SET product_image = '{$imgFinalPath}' WHERE product_sku = '{$product->getSku()}'";
				$writeConnection->query($query);
			}
		}
		$products1 = Mage::helper('promoteevents')->getEvents('fashion_products_collection')->getRecommandProducts();
		foreach($products1 as $pro1)
		{
			foreach($pro1 as $product1)
			{
				$imgFullUrl = Mage::helper('catalog/image')->init($product1, 'small_image')->resize(320);
				$imgBasePath = substr($imgFullUrl,$homeUrlLength);
				$imgFinalPath = str_replace('cache','events',$imgBasePath);
				echo $imgFullUrl .'<br>';
				
				$query = "UPDATE `rumall_promote_events_product` SET product_image = '{$imgFinalPath}' WHERE product_sku = '{$product->getSku()}'";
				$writeConnection->query($query);
			}
		}
		$products = Mage::helper('promoteevents')->getEvents('3c_products_collection')->getProducts();
		foreach($products as $pro)
		{
			foreach($pro as $product)
			{
				$imgFullUrl = Mage::helper('catalog/image')->init($product, 'small_image')->resize(190);
				$imgBasePath = substr($imgFullUrl,$homeUrlLength);
				$imgFinalPath = str_replace('cache','events',$imgBasePath);
				echo $imgFullUrl .'<br>';
				
				$query = "UPDATE `rumall_promote_events_product` SET product_image = '{$imgFinalPath}' WHERE product_sku = '{$product->getSku()}'";
				$writeConnection->query($query);
			}
		}
		$products1 = Mage::helper('promoteevents')->getEvents('3c_products_collection')->getRecommandProducts();
		foreach($products1 as $pro1)
		{
			foreach($pro1 as $product1)
			{
				$imgFullUrl = Mage::helper('catalog/image')->init($product1, 'small_image')->resize(320);
				$imgBasePath = substr($imgFullUrl,$homeUrlLength);
				$imgFinalPath = str_replace('cache','events',$imgBasePath);
				echo $imgFullUrl .'<br>';
				
				$query = "UPDATE `rumall_promote_events_product` SET product_image = '{$imgFinalPath}' WHERE product_sku = '{$product->getSku()}'";
				$writeConnection->query($query);
			}
		}
	}

}